﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using TrackSystem.Contracts;
using TrackSystem.Entities.Dtos.Equip;
using TrackSystem.IO.Data;
using TrackSystem.IO.Formula;
using TrackSystem.IO.Sensors;
using TrackSystem.IO.Utils;

namespace TrackSystem.API.Controllers
{
    /// <summary>
    /// IO HUB控制器
    /// </summary>
    [Route("api/io")]
    [ApiController]
    public class IOController : ControllerBase
    {
        private readonly IRepositoryWrapper _wrapper;
        private readonly ILogger<ChannelController> _logger;
        private readonly IMapper _mapper;

        /// <summary>
        /// 通道控制器构造函数
        /// </summary>
        /// <param name="wrapper"></param>
        /// <param name="logger"></param>
        /// <param name="mapper"></param>
        public IOController(IRepositoryWrapper wrapper,
            ILogger<ChannelController> logger,
            IMapper mapper)
        {
            this._wrapper = wrapper;
            this._logger = logger;
            _mapper = mapper;
        }
        /// <summary>
        /// 获取所有通道信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("Sensor")]
        public async Task<ResultMsg> InitSensor()
        {
            try
            {
                var sensors = SensorFactory.Instance;
                return await sensors.Load(_wrapper);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "[传感器] 初始化异常");
                return ResultMsg.Fail($"[传感器] 初始化异常, {ex.Message}");
            }
        }

        /// <summary>
        /// 获取所有通道信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("Reg")]
        public ResultMsg InitRegs()
        {
            try
            {
                var regs = RegFactory.Instance;
                return regs.Load(_wrapper);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "[寄存器] 初始化异常");
                return ResultMsg.Fail($"[寄存器] 初始化异常, {ex.Message}");
            }
        }

    }
}
